package com.springboot.demo;

import com.springboot.demo.module.entity.TSysUser;
import com.springboot.demo.module.service.IUserService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.concurrent.TimeUnit;

/**
 * 测试redis
 *
 * @author Francis
 * @version 1.0
 * @date 2019-07-05 9:43
 */
public class Demo1 extends DemoApplicationTests{

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    IUserService userService;


    /**
     * 【基础篇】redis 测试 -- 1、字符串键值对存入
     */
    @Test
    public void method1(){
        //设置 key-value 序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());

        redisTemplate.opsForValue().set("key", "测试redis是否联通");
    }


    /**
     * 【基础篇】redis 测试 -- 2、字符串键值对取出
     */
    @Test
    public void method2(){
        //设置 key-value 序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());

        String value = (String) this.redisTemplate.opsForValue().get("key");
        System.out.println(value);
    }


    /**
     * 【基础篇】redis 测试 -- 3、对象的存入（仅demo，占用空间是存入json的5倍以上）
     * PS：1、对象必须实现序列化接口。2、需对value重新设置序列化器
     */
    @Test
    public void method3(){
        //设置 key-value 序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());

        TSysUser user = userService.list(null).get(0);

        redisTemplate.opsForValue().set("user", user);
    }

    /**
     * 【基础篇】redis 测试 -- 4、对象的取出
     * PS：1、对象必须实现序列化接口。2、需对value重新设置序列化器
     */
    @Test
    public void method4(){
        //设置 key-value 序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());

        TSysUser user = (TSysUser) this.redisTemplate.opsForValue().get("user");
        System.out.println(user);
    }


    /**
     * 【基础篇】redis 测试 -- 5、json对象的存入
     */
    @Test
    public void method5(){
        //设置 key-value 序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(TSysUser.class));

        TSysUser user = userService.list(null).get(0);

        redisTemplate.opsForValue().set("userJson", user);
    }

    /**
     * 【基础篇】redis 测试 -- 6、json对象的取出
     */
    @Test
    public void method6(){
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(TSysUser.class));

        TSysUser user = (TSysUser) this.redisTemplate.opsForValue().get("userJson");
        System.out.println(user);
    }


    /**
     * 【基础篇】redis 测试 -- 设定key的同时设定失效时间
     */
    @Test
    public void method7(){
        //设置 key-value 序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());

        redisTemplate.opsForValue().set("key2", "测试失效时间", 10, TimeUnit.SECONDS);
    }

    /**
     * 【基础篇】redis 测试 -- 给现有key设置失效时间
     */
    @Test
    public void method8(){
        //设置 key-value 序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());

        redisTemplate.expire("key", 10, TimeUnit.SECONDS);
    }

}
